<?php
namespace Home\Controller;
use Think\Controller;
/**
 *  游戏管理
 */
class GameManController extends \Home\Common\BaseAdminController {
	//玩家列表页面
	public function index() {
		$data = $this->GetOnlineNum()['content'];
		$this->assign('users',$data['list']);
		$this->display();
	}
	//玩家充值接口
	public function add_pay_info() {
		$data = array(
			'code'=>0,
			'msg' =>'请求成功',
		);
		if(IS_POST){
			//$post[''] = $_POST('');
		
			$this->ajaxReturn($data);
		} else {
			$data['code'] = -1;
			$data['msg'] = '非法请求';
			$data['data'] = '' ;
			$this->ajaxReturn($data);
		}
	}
	//获取线上人数列表
	public function GetOnlineNum() {
		$res = array();
		$res = PostGameCUrl('192.168.0.103:8901/GetOnlineNum',array('NickName'=>''));//田博文
//		$res = PostGameCUrl('192.168.0.193:1011/GetOnlineNum',array('NickName'=>''));//张志远

		return $res;
	}
	//玩家强制下线
	public function player_out() {
		$this->display();
	}
	//玩家列表
	public function users(){
		$where = ' 1=1 ';
		$NickName = I('player','');
		if($NickName){
			$where .=" and `NickName` like '{$NickName}%'";
		}
		$player = CreateGameModel('player');//实例化数据对象
		$count= $player->where($where)->count();// 查询满足要求的总记录数
		$p = getPage($count,10);
		$play  = CreateGameModel('player');
		$tmp = $play->where($where)->order('CreateTime desc')->limit($p->firstRow, $p->listRows)->select();
		$this->assign('name',$NickName);
		$this->assign('list',$tmp);// 赋值数据集
		$this->assign('page',$p->show());// 赋值分页输出
		$this->display();
	}
	/**************************福利发放**************************************/
	//福利发放
	public function welfare(){
		$welfare = C('welfare');
		$this->assign('welfare',$welfare);//获取福利列表
		$this->display();
	}
	//ajax请求玩家信息
	public function get_wefare(){
		$name = I('name','');
		$data = ["code"=>0,"msg"=>"请求成功","data"=>""];
		if(!$name){
			$data['code'] = -1;
			$data['msg'] = '请输入玩家昵称';
			$this->ajaxReturn($data);
		}
		$res = PostGameCUrl('192.168.0.103:8901/GetPlayerInfo',array('NickName'=>$name)); //田博文
//		$res = PostGameCUrl('192.168.0.193:1011/GetPlayerInfo',array('NickName'=>$name));//张志远
//		dump($res);exit;
		if(!$res['content']){
			$data['code'] = -1;
			$data['msg'] = "该玩家昵称不存在，请确认信息后重新输入";
			$this->ajaxReturn($data);
		}
		$res['content']['ceer'] = $this->getPlayerCareer($res['content']['Career']); 
		$res['content']['se'] = $this->getPlayerSex($res['content']['Sex']); 
		$this->ajaxReturn($res);
	}
	//获取货币id
	public function welfare_id(){
		$type_name = I('type_id','');
		$type_id = 0;
		$welfare = C('welfare');
		if($type_name){
			$type_id = array_search($type_name, $welfare)+1;
		}
		return $type_id;
	}
	//开始发放福利
	public function add_welfare(){
		$data = ['code'=>0,'msg'=>"提交成功,审核成功后会发放给玩家"];
		$type_name = I('type_name','');
		$num = I('num',0);
		$player_name = I("player_names",'');
		$desc = I('desc','');
		if(!$num){
			$data['code']=-1;
			$data['msg'] = "请输入发放数量";
			$this->ajaxReturn($data);
		}
		if(!$type_name){
			$data['code']=-1;
			$data['msg'] = "请选择需要发放的福利";
			$this->ajaxReturn($data);
		}
		if(!$player_name){
			$data['code'] = -1;
			$data['msg']  = "玩家昵称不能为空";
			$this->ajaxReturn($data);
		}
		$player_name = str_replace(array("\r\n", "\r", "\n"," ","	"), ",", $player_name);
		$player_name = preg_replace('/(,)+/i',',',$player_name);
		$player_name = trim($player_name, ',');
		$names = explode(',',$player_name);
		$result = [];
		$user_id = (int)$_SESSION['user_data']['user_id'];
		$user_name = $_SESSION['user_data']['user_nick'];
		$config = C('welfare');
		$welfare_id = array_search($type_name, $config);
		$welfare_id += 1;
		foreach($names as $k=>$v){
			if($v){
				$result[$k]['user_id']      = $user_id;
				$result[$k]['user_name']    = $user_name;
				$result[$k]['order_num'] 	= 'DPCQF' . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
				$result[$k]['welfare_id'] 	= $welfare_id;
				$result[$k]['welfare_name'] = $type_name;
				$result[$k]['welfare_num']  = (int)$num;
				$result[$k]['player_name']  = $v;
				$result[$k]['is_show']		= 0;
				$result[$k]['enabled']		= 0;
				$result[$k]['welfare_desc'] = $desc;
			}
		}
		$order_id = $this->add_welfare_log($result);
		if(!$order_id){
			$data['code'] = -1;
			$data['msg']  = "log日志写入失败";
			$this->ajaxReturn($data);
		}
		$this->ajaxReturn($data);
	}
	//实际发放接口 审核接口
	public function welfare_success(){
		$data = array('code'=>0,'msg'=>"发放成功");
		$order_id = I('order_id',0);
		$model = M('welfare_log');//修改log日志
		$where = '';
		if(!$order_id){
			$data['code'] = -1;
			$data['msg']  = '请确认审核的福利发放';
			$this->ajaxReturn($data);
		}
		$where .=' log_id = '.$order_id.' ';
		$model->enabled = 1;
		$tmp = $model->where($where)->save();
		if(!$tmp){
			$data['code'] = -1;
			$data['msg']  = '审核失败，请稍后再试';
			$this->ajaxReturn($data);
		}
		$res = $model->where($where)->find();
		$config = C('welfare');
		$pay_type = array_search($res['welfare_name'], $config);
		$curl = [];
		$curl['NickName'] = $res['player_name'];
		$curl['OrderID']  = $res['order_num'];
		$curl['PayMoney'] = $res['welfare_num'];
		$curl['PayType']  = (int)$pay_type;
		$bool = PostGameCUrl('192.168.0.103:8901/GiveMoney',$curl);//田博文
//		$bool = PostGameCUrl('192.168.0.193:1011/GiveMoney',$curl);//张志远
		$audit = M('audit_log');//审核日志
		$arr = [];
		$arr['audit_type'] = 1;
		$arr['audit_status'] = $bool['code'];
		$arr['audit_msg'] = $bool['msg'];
		$arr['user_id'] = (int)$_SESSION['user_data']['user_id'];
		$arr['user_group'] = $_SESSION['user_group']['group_name'];
		$arr['user_name'] = $_SESSION['user_data']['user_nick'];
		$arr['log_id'] = $order_id;
		$audit->data($arr)->add();
		if($bool['msg'] == "找不到该玩家!"){
			$model->is_show = 1;
		}else{
			$model->audit_status = 1;
		}
		$model->audit_name = $_SESSION['user_data']['user_nick'];
		$model->where($where)->save();
		$this->ajaxReturn($bool);
	}
	// 福利日志页面
	public function wel_logs(){
		$welfare = M('welfare_log');
        $where = ' (is_show=0)';
        $nickname = I('name','');
		//查询条件
        if($nickname){
            $where .= " and player_name like '%".$nickname."%'";
        }
		//数据总数分页
        $count = $welfare->where($where)->count();
        $p = getPage($count,15);//每页显示条数
		//分页数据查询
        $list = $welfare->where($where)->order('create_time desc')
        ->limit($p->firstRow, $p->listRows)->select();
		//数据渲染
		foreach($list as $k=>$v){
			$list[$k]['create'] = substr($v['create_time'],6,17);
			$list[$k]['update'] = substr($v['update_time'],6,17);
		}
        $this->assign('list',$list);
        $this->assign('page',$p->show());
        $this->assign('name',$nickname);
		$this->display('welfareLog');
	}
	/**************************物品发放**************************************/
	//物品发放
	public function article(){
		$this->display();
	}
	//获取物品信息
	public function get_items(){
		$data = ['code'=>1,'msg'=>'请输入物品名称','content'=>''];
		$item_name = I('name','');
		if(!$item_name) $this->ajaxReturn($data);
		$where = '';
		$where .= " `name` like '%{$item_name}%'";
		$item = M('item_tpl');
		$tmp = $item->field('tid,`name`,can_business')->where($where)->select();
		$data['code'] = 0;
		$data['msg'] = '请求成功';
		if($tmp){
			$data['content'] = $tmp;
		}else{
			$data['code'] = -1;
			$data['msg'] = '没有找到该商品';
			$data['content'] = [];
		}
		$this->ajaxReturn($data);
	}
	//物品信息申请
	public function itmes_info(){
		$data = ['code'=>-1,'msg'=>'请求失败','content'=>''];
		$player_name = I('name','');
		$title = I('email_title','');
		$content = I('email_content','');
		$item_list = I('list',[]);
		$remark = I('desc','');
		$arr = [];
		if(empty($player_name)){
			$data['msg']='请输入玩家昵称';
			$this->ajaxReturn($data);
		}
		if(empty($title)){
			$data['msg']='请输入标题';
			$this->ajaxReturn($data);
		}
		if(empty($content)){
			$data['msg']='请输入标题内容';
			$this->ajaxReturn($data);
		}
		if(count($item_list)<1){
			$data['msg']='请选择需要发放的物品';
			$this->ajaxReturn($data);
		}
		$order_number = 'DPCQW' . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
		$arr['order_num'] 	= $order_number;
		$arr['user_id'] = (int)$_SESSION['user_data']['user_id'];
		$arr['user_name'] = $_SESSION['user_data']['user_nick'];
		$arr['player_name'] = $player_name;
		$arr['email_title'] = $title;
		$arr['email_content'] = $content;
		$arr['desc'] = $remark;
		$order_id = M('item_log')->add($arr);
		if(!$order_id){
			$data['code']=-1;
			$data['msg'] ="日志写入失败";
			$this->ajaxReturn($data);
		}
		$desc = [];
		for($i=0;$i<count($item_list);$i++){
			$str = explode('|',$item_list[$i]);
			$res = M('item_tpl')->field('tid,`name`,item_type,can_business,item_icon')->where("tid={$str[0]}")->find();
			$desc[$i]['item_log_id'] = $order_id;
			$desc[$i]['item_type'] = 1;//名称
			$desc[$i]['is_band'] = (int)$res['can_business'];//是否绑定
			$desc[$i]['item_model'] = $res['tid'];//模版
			$desc[$i]['item_num'] = (int)$str[2];//数量
			$desc[$i]['item_name'] = $res['name'];//名称
		}
		$tmp = M('article_desc')->addAll($desc);
		if($tmp){
			$data['code'] = 0;
			$data['msg'] = '申请成功，审核通过后会及时发放给玩家';
			$this->ajaxReturn($data);
		}
		$data['msg'] ="关系表写入错误";
		$this->ajaxReturn($data);
	}
	//物品日志页面
	public function art_log(){
		$item = M('item_log');
		$where = ' (is_show=0)';
		$nickname = I('name','');
		//查询条件
		if($nickname){
			$where .= " and player_name like '%".$nickname."%'";
		}
		//数据总数分页
		$count = $item->where($where)->count();
		$p = getPage($count,15);//每页显示条数
		//分页数据查询
		$list = $item->where($where)->order('create_time desc')
			->limit($p->firstRow, $p->listRows)->select();
		//数据渲染
		foreach($list as $k=>$v){
			$item_d = [];
			$log_id = (int)$v['id'];
			$item_desc = M('article_desc')->field('item_model,item_name,item_num')->where("item_log_id={$log_id}")->select();

			foreach($item_desc as $key=>$val){
				$item_d[$key] = implode('|',$val);
			}
			$list[$k]['item_desc'] = $item_d;
			unset($item_d);
		}
		foreach($list as $k=>$v){
			$list[$k]['create'] = substr($v['create_time'],6,17);
			$list[$k]['update'] = substr($v['update_time'],6,17);
		}
		$this->assign('list',$list);
		$this->assign('page',$p->show());
		$this->assign('name',$nickname);
		$this->display('articleLog');
	}
	//物品审核发放接口
	public function article_success(){
		$data = array('code'=>0,'msg'=>"发放成功");
		$order_id = I('log',0);
		$model = M('item_log');//修改log日志
		$where = '';
		if(!$order_id){
			$data['code'] = -1;
			$data['msg']  = '请确认审核的物品发放';
			$this->ajaxReturn($data);
		}
		$where .=' id = '.$order_id.' ';
		$model->enabled = 1;
		$tmp = $model->where($where)->save();
		if(!$tmp){
			$data['code'] = -1;
			$data['msg']  = '审核失败，请稍后再试';
			$this->ajaxReturn($data);
		}
		$item_d = [];
		$res = $model->field('player_name,email_title,email_content')->where($where)->find();
		$item_desc = M('article_desc')->field('item_type,is_band,item_model,item_num')->where("item_log_id={$order_id}")->select();
		foreach($item_desc as $key=>$val){
			$item_d[$key] = implode(',',$val);
		}
		$str = implode('|',$item_d);
		$curl = [];
		$curl['NickName'] = $res['player_name'];
		$curl['Title'] = $res['email_title'];
		$curl['Content'] = $res['email_content'];
		$curl['ItemStr'] = $str;
		$bool = PostGameCUrl('192.168.0.103:8901/GiveMoney',$curl);//田博文
//		$bool = PostGameCUrl('192.168.0.193:1011/SendItemMail',$curl);//张志远
		if(!$bool){
			$data['code'] = -1;
			$data['msg'] = "远程服务器连接失败";
			$model->enabled = 0;
			$model->where($where)->save();
			$this->ajaxReturn($data);
		}
		$audit = M('audit_log');//审核日志
		$arr = [];
		$arr['audit_type'] = 2;
		$arr['audit_status'] = $bool['code'];
		$arr['audit_msg'] = $bool['msg'];
		$arr['user_id'] = (int)$_SESSION['user_data']['user_id'];
		$arr['user_group'] = $_SESSION['user_group']['group_name'];
		$arr['user_name'] = $_SESSION['user_data']['user_nick'];
		$arr['log_id'] = $order_id;
		$audit->data($arr)->add();
		if($bool['msg'] == "找不到该玩家!"){
			$model->is_show = 1;
		}else{
			$model->audit_status = 1;
		}
		$model->audit_name = $_SESSION['user_data']['user_nick'];
		$model->where($where)->save();
		$this->ajaxReturn($bool);
	}
	/**************************禁止开放等接口开始**************************************/
	//禁ip 禁言等接口
	public function ajax_return(){
		$data = ['code'=>-1,'msg'=>'请求失败','data'=>''];
		$url = I('u','');
		$arr_name = I('player',[]);
		$curl = [];
		if(!$url){
			$data['msg'] = "url没有获取到";
			$this->ajaxReturn($data);
		}
		if(!$arr_name){
			$data['msg'] = "请选择玩家";
			$this->ajaxReturn($data);
		}else{
			foreach($arr_name as $k=>$v){
				$curl[$k]['NickName'] = $v;
			}
		}
		//封ip
		if(($url == 'OpenLoginIP') || ($url == 'SealLoginIP') ){
			foreach($curl as $k=>$v){
				$curl[$k]['LoginIP'] = CreateGameModel('player')->field('LoginIP')->where("NickName='{$v['NickName']}'")->find()['LoginIP'];
			}
		}
		//GM 命令
		if($url == 'GMRightsHttp'){
			foreach($curl as $k=>$v){
				$gm_status = CreateGameModel('player')->field('GMRights')->where("NickName='{$v['NickName']}'")->find()['GMRights'];
				if($gm_status){
					$curl[$k]['rights'] = 0;
				}else{
					$curl[$k]['rights'] = 1;
				}
			}
		}
		//隐身
		if($url == 'StealthPlayer'){
			foreach($curl as $k=>$v){
				$curl[$k]['StealthType'] = 0;
			}
		}
		//取消隐身
		if($url == 'StealthPlayer1'){
			$url = "StealthPlayer";
			foreach($curl as $k=>$v){
				$curl[$k]['StealthType'] = 1;
			}
		}
		for($i=0;$i<count($curl);$i++){
			$res = PostGameCUrl("192.168.0.103:8901/{$url}",$curl[$i]);//田博文
//			$res = PostGameCUrl("192.168.0.193:1011/{$url}",$curl[$i]);//张志远
			if(!$res){
				$data['msg'] = "远程服务器连接失败";
				$this->ajaxReturn($data);
			}
		}
		$data['code'] = 0;
		$data['msg'] = "操作成功";
		$this->ajaxReturn($data);
	}
	//角色属性
	public function PlayerPropertyInfo(){
		$nick_name = I('player',[]);
		$data = ['code'=>-1,'msg'=>'请求失败','data'=>''];
		if(!$nick_name){
			$data['msg'] = '玩家昵称不能为空';
			$this->ajaxReturn($data);
		}
		$name = $nick_name[0];
		$bool = PostGameCUrl('192.168.0.103:8901/PlayerPropertyInfo',['NickName'=>$name]);//田博文
//		$bool = PostGameCUrl('192.168.0.193:1011/PlayerPropertyInfo',['NickName'=>$name]);//张志远
		if($bool){
			$data['code'] = $bool['code'];
			$data['msg'] = $bool['msg'];
			$data['data'] = $bool['content'];
		}else{
			$data['msg'] = '远程服务器链接失败';
		}
		$this->ajaxReturn($data);
	}
	//获取角色属性页面
	public function getPlayerInfo(){
		$player = I('player','');
		$player_info=[];
		if(count($player)>0){
			if(is_array($player)){
				$play = $player[0];
			}else{
				$play = $player;
			}
			$curl = PostGameCUrl('192.168.0.103:8901/PlayerPropertyInfo',['NickName'=>$play]);//田博文
//			$curl = PostGameCUrl('192.168.0.193:1011/PlayerPropertyInfo',['NickName'=>$play]);//张志远
			if($curl){
				$player_info = $curl['content'];
			}
			$this->assign('info',$player_info);
		}
		$this->display('player');
	}
	/**************************自定义方法**************************************/
	//获取性别
	function getPlayerSex($sex){
		if(empty($sex)) return '男';
		switch ($sex) {
			case 1:
				$sex = '男';
				break;
				# code...
			case 2:
				$sex = '女';
				break;
		}
		return $sex;
	}
	//获取职业
	function getPlayerCareer($career){
		if(empty($career)) return '战士';
		switch ($career) {
			case 1:
				# code...
				$career = '战士';
				break;
			case 2:
				# code...
				$career = '法士';
				break;
			case 3:
				# code...
				$career = '道士';
				break;	
		}
		return $career;
	}
	//写日志
	function add_welfare_log($data){
		$log = M('welfare_log');
		$bool = $log->addAll($data);
		return $bool;
	}
}